import {
  getKnowledgeListByRobotId,
  getAtlasListByRobotId,
  getDialogListByRobotId,
  getFileListByRobotId,
  getKeyWordListByRobotId,
  saveKnowledgeIds,
  saveAtlasIds,
  saveDialogIds, saveFileIds,
  saveKeyWordIds,
  saveFormIds
} from '@/services/knowledgeConfiguration'

export default {
  namespace: 'knowledgeConfiguration',
  state: {
    data: {
      qaList: [],
      total: 0,
      suggestList: [],
    },
  },

  effects: {
    *fetchKnowledgeList({payload,callback}, { call, put }) {
      const response = yield call(getKnowledgeListByRobotId,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchAtlasList({payload,callback}, { call, put }) {
      const response = yield call(getAtlasListByRobotId,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchDialogList({payload,callback}, { call, put }) {
      const response = yield call(getDialogListByRobotId,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchKeyWordList({payload,callback}, { call, put }) {
      const response = yield call(getKeyWordListByRobotId,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchFileList({payload,callback}, { call, put }) {
      const response = yield call(getFileListByRobotId,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *saveCheckedKnowledgeIds({payload,callback}, { call, put }) {
      const response = yield call(saveKnowledgeIds,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *saveCheckedAtlasIds({payload,callback}, { call, put }) {
      const response = yield call(saveAtlasIds,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *saveCheckedDialogIds({payload,callback}, { call, put }) {
      const response = yield call(saveDialogIds,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *saveCheckedFileIds({payload,callback}, { call, put }) {
      const response = yield call(saveFileIds,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *saveCheckedKeyWordIds({payload,callback}, { call, put }) {
      const response = yield call(saveKeyWordIds,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *saveCheckedFormIds({payload,callback}, { call, put }) {
      const response = yield call(saveFormIds,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
